home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / C / LIB / UNIXLIB37B / !UnixLib37 / src / unix / c / open < prev    next >
Text File  |  1996-11-09  |  2KB  |  105 lines

  1. /****************************************************************************
  2.  *
  3.  * $Source: /unixb/home/unixlib/source/unixlib37/src/unix/c/RCS/open,v $
  4.  * $Date: 1996/10/30 21:59:01 $
  5.  * $Revision: 1.2 $
  6.  * $State: Rel $
  7.  * $Author: unixlib $
  8.  *
  9.  * $Log: open,v $
  10.  * Revision 1.2  1996/10/30 21:59:01  unixlib
  11.  * Massive changes made by Nick Burret and Peter Burwood.
  12.  *
  13.  * Revision 1.1  1996/04/19 21:35:27  simon
  14.  * Initial revision
  15.  *
  16.  ***************************************************************************/
  17.  
  18. static const char rcs_id[] = "$Id: open,v 1.2 1996/10/30 21:59:01 unixlib Rel $";
  19.  
  20. #include <stdarg.h>
  21. #include <errno.h>
  22. #include <fcntl.h>
  23. #include <unistd.h>
  24.  
  25. #include <sys/syslib.h>
  26. #include <sys/unix.h>
  27. #include <sys/dev.h>
  28. #include <sys/types.h>
  29.  
  30. struct sfile __sfile[] =
  31. {
  32.   {"tty", makedev (DEV_TTY, 0)},
  33.   {"console", makedev (DEV_TTY, 0)},
  34.   {"rs423", makedev (DEV_TTY, 0)},
  35.   {"null", makedev (DEV_NULL, 0)},
  36.   {0, makedev (DEV_RISCOS, 0)}};    /* table terminator */
  37.  
  38. char *
  39. ttyname (int fd)
  40. {
  41.   fd = fd;
  42.   return ("/dev/tty");
  43. }
  44.  
  45. int
  46. open (const char *file, int oflag, ...)
  47. {
  48.   register struct file *f;
  49.   va_list ap;
  50.   register int mode;
  51.   int fd;
  52.   dev_t dev;
  53.  
  54. /* __uname() is called by __fsopen() */
  55.  
  56.   if (oflag & O_CREAT)
  57.     {
  58.       va_start (ap, oflag);
  59.       mode = va_arg (ap, int);
  60.       va_end (ap);
  61.     }
  62.   else
  63.     mode = 0777;
  64.  
  65.   if ((fd = __fdalloc ()) < 0)
  66.     return (-1);
  67.  
  68.   f = __u->file + fd;
  69.   f->oflag = oflag;
  70.  
  71.   if (file[0] == '/' && file[1] == 'd' && file[2] == 'e' &&
  72.       file[3] == 'v' && file[4] == '/')
  73.     {
  74.       register struct sfile *s = __sfile;
  75.       register char *s1, *s2;
  76.  
  77.       while (s1 = s->name)
  78.     {
  79.       s2 = (char *)file + 5;
  80.  
  81.       while (*s1 && *s2 && *s1 == *s2)
  82.         s1++, s2++;
  83.       if (*s1 == *s2)
  84.         break;
  85.       s++;
  86.     }
  87.       dev = s->dev;
  88.     }
  89.   else
  90.     dev = makedev (DEV_RISCOS, 0);
  91.  
  92.   {
  93.     register int i;
  94.  
  95.     if ((i = __funcall ((*(__dev[major (dev)].open)), ((char *)file, mode, f))) < 0)
  96.       return (-1);
  97.     f->dev = makedev (major (dev), i);
  98.   }
  99.  
  100.   f->dup = f;
  101.   f->pid = __u->pid;
  102.  
  103.   return (fd);
  104. }
  105.